//
//  AdSuyiSuspensionViewController.m
//  ADSuyiDev
//
//  Created by 刘娟 on 2025/5/12.
//

#import "AdSuyiSuspensionViewController.h"
#import <MJRefresh/MJRefresh.h>
#import "UIView+Toast.h"
#import <ADSuyiSDK/ADSuyiSDKSuspensionAdView.h>
#import "AdMacros.h"
@interface AdSuyiSuspensionViewController () <UITableViewDelegate, UITableViewDataSource, ADSuyiSDKSuspensionAdViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *items;

@property (nonatomic, strong) ADSuyiSDKSuspensionAdView *suspensionAdView;

@end

@implementation AdSuyiSuspensionViewController

#pragma mark - Life crycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    _items = [NSMutableArray new];
    
    _tableView = [UITableView new];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    _tableView.frame = self.view.bounds;
    
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf cleanAllAd];
        [weakSelf loadNative];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadNative];
    }];
    
    [self.tableView.mj_header beginRefreshing];
    
    self.suspensionAdView = [[ADSuyiSDKSuspensionAdView alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 100 - 10, self.view.frame.size.height/2 + 150, 100, 120)];
    self.suspensionAdView.delegate = self;
    self.suspensionAdView.posId = Suyi_Demo_flowAd_suspension_PosId;
    [self.suspensionAdView loadAndShow:self];
    [self.view addSubview:self.suspensionAdView];
    self.view.userInteractionEnabled = YES;
}

- (void)dealloc {
    NSLog(@"%s",__func__);
    [self cleanAllAd];
}
#pragma mark - Private method

- (void)cleanAllAd {
    _items = [NSMutableArray new];
    [self.tableView reloadData];
}

- (void)loadNative{
    for (int i = 0; i < 15; i ++) {
        [self.items addObject:[NSNull null]];
    }
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    [self.tableView reloadData];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [UIView animateWithDuration:1 animations:^{
        self.suspensionAdView.frame = CGRectMake(self.view.frame.size.width - 40, self.suspensionAdView.frame.origin.y, self.suspensionAdView.frame.size.width, self.suspensionAdView.frame.size.height);
    }];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (!decelerate) {
        [UIView animateWithDuration:1 animations:^{
            self.suspensionAdView.frame = CGRectMake(self.view.frame.size.width - self.suspensionAdView.frame.size.width - 10, self.suspensionAdView.frame.origin.y, self.suspensionAdView.frame.size.width, self.suspensionAdView.frame.size.height);
        }];
    }
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [UIView animateWithDuration:1 animations:^{
        self.suspensionAdView.frame = CGRectMake(self.view.frame.size.width - self.suspensionAdView.frame.size.width - 10, self.suspensionAdView.frame.origin.y, self.suspensionAdView.frame.size.width, self.suspensionAdView.frame.size.height);
    }];
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 64;
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.items.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    id item = [_items objectAtIndex:indexPath.row];
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.text = [NSString stringWithFormat:@"ListViewitem %li", indexPath.row];
        return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
#pragma mark - ADSuyiSDKSuspensionAdViewDelegate

- (void)adsy_suspensionViewDidReceived:(nonnull ADSuyiSDKSuspensionAdView *)suspensionView {
    ADSuyiSDKExtInfo *extInfo = [suspensionView adsy_extInfo];
    NSLog(@"%s,ecpm=%@,ecpmType=%ld",__func__,extInfo.ecpm,extInfo.ecpmType);
}

- (void)adsy_suspensionViewFailToReceived:(nonnull ADSuyiSDKSuspensionAdView *)suspensionView errorModel:(nonnull ADSuyiAdapterErrorDefine *)errorModel {
    NSLog(@"%s:%@",__func__,errorModel.errorDescription);
}

- (void)adsy_suspensionViewDidPresent:(nonnull ADSuyiSDKSuspensionAdView *)suspensionView {
    NSLog(@"%s",__func__);
}

- (void)adsy_suspensionViewExposure:(nonnull ADSuyiSDKSuspensionAdView *)suspensionView {
    NSLog(@"%s",__func__);
}

- (void)adsy_suspensionViewClicked:(nonnull ADSuyiSDKSuspensionAdView *)suspensionView { 
    NSLog(@"%s",__func__);
}

- (void)adsy_suspensionViewClose:(nonnull ADSuyiSDKSuspensionAdView *)suspensionView { 
    NSLog(@"%s",__func__);
    _suspensionAdView = nil;
}

- (void)adsy_suspensionAdCloseLandingPage:(nonnull ADSuyiSDKSuspensionAdView *)suspensionView {
    NSLog(@"%s",__func__);
}

@end
